package com.android.filter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.MediaStore;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.android.filter.jni.EffectJNI;

public class MosaicViewPagerAdapterClass extends PagerAdapter{

	public MosaicViewPagerAdapterClass(Context context)
	{
		super();
		mViewList = new ArrayList<GridView>();
		for(int i = 0 ; i < NUM_VIEW ; i++)
		{
			GridView v = new GridView(context);
			if(i == 0)
				v.setId(EffectJNI.SQUARES);
			else if(i == 1)
				v.setId(EffectJNI.HEXAGONS);
			else if(i == 2)
				v.setId(EffectJNI.OCTAGONS);
			else
				v.setId(EffectJNI.TRIANGLES);
			mViewList.add(v);
		}
		mCurrentGridView = mViewList.get(0);
	}
	
	public void init(int width, int height)
	{
		mWidth = width;
		mHeight = height;
	}
	public void save(ContentResolver cr, String filePath, String fileName)
	{
		if(mCurrentGridView != null)
		{
			PreviewImage applyImage = mCurrentGridView.getMosaicApplyImage();
			if(applyImage != null)
			{
				File saveFile = new File(filePath + fileName);
				OutputStream os = null;
				try {
					os = new FileOutputStream(saveFile);
					applyImage.saveBitmap(os);
					MediaStore.Images.Media.insertImage(cr, filePath + fileName, fileName, fileName);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return NUM_VIEW;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position)
	{
		GridView ret = mViewList.get(position);
		ret.init( mWidth, mHeight);
		((ViewPager)container).addView(ret, 0);
		return ret;
	}
	@Override
    public void destroyItem(View pager, int position, Object view) {    
        ((ViewPager)pager).removeView((View)view);
//        ((GridView)view).destroy();
    }

    @Override
    public boolean isViewFromObject(View pager, Object obj) {
        return pager == obj; 
    }
    public void destroy()
    {
    	for(GridView v: mViewList)
    		v.destroy();
    	mViewList.clear();
    	mViewList = null;
    }
    public void startDrag(MosaicView mosaicView)
    {
    	PreviewImage preview = mCurrentGridView.getPreviewImage();
    	if(preview != null)
    	{
    		mosaicView.setPreviewImage(preview);
    		mCurrentGridView.setNullPreviewImage();
    	}
    }
    public void resetOther(int position, MosaicView mosaicView)
    {
    	mCurrentGridView = mViewList.get(position);
		if(mosaicView != null)
		{
			PreviewImage preview = mosaicView.getPreviewImage();
			mCurrentGridView.setPreviewImage(preview);
			mosaicView.setNullPreviewImage();
		}
    	for(GridView v: mViewList)
    	{
    		if(mCurrentGridView != v)
    			v.init( mWidth, mHeight);
		}
    }
    private GridView mCurrentGridView = null;
    private ArrayList<GridView> mViewList = null;
    private int mWidth, mHeight;
    private final int NUM_VIEW = 4;
}
